#include <iostream>
#include <vector>
#include <algorithm>
int n=0;
const int N=2e5+10;
int arr[N]={0};
int num[11]={1,0,0,0,1,0,1,0,2,1};
using namespace std;
int CountNum(int x){
	int count=0;
	while(x){
		count+=num[x%10];
		x/=10;
	}
	return count;
}
bool compare(int x,int y){
	int tempx=CountNum(x),tempy=CountNum(y);
	if(tempx!=tempy) return tempx<tempy;
	else return x<y;
}
int main(){
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>arr[i];	
	sort(arr,arr+n,compare);
	for(int i=0;i<n;i++)
	cout<<arr[i]<<' ';
	cout<<endl;		
	return 0;
}
